﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;

public partial class Admin_ManageUser : System.Web.UI.Page
{
    private MembershipUserCollection allUsers = Membership.GetAllUsers();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            lblTotUsers.Text = allUsers.Count.ToString();
            lblOnlineUsers.Text = Membership.GetNumberOfUsersOnline().ToString();

            string[] alphabet = "A;B;C;D;E;F;G;H;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(';');
            rptAlphabet.DataSource = alphabet;
            rptAlphabet.DataBind();
        }
    }
    private void BindUser(bool reloadAllUser)
    {
        if (reloadAllUser)
            allUsers = Membership.GetAllUsers();

        MembershipUserCollection users;
        string searchText = "";
        if (!string.IsNullOrEmpty(gvwUsers.Attributes["SearchText"]))
            searchText = gvwUsers.Attributes["SearchText"];

        bool searchEmail = false;
        if (!string.IsNullOrEmpty(gvwUsers.Attributes["SearchByEmail"]))
            searchEmail = bool.Parse(gvwUsers.Attributes["SearchByEmail"]);

        if (searchText.Length > 0)
        {
            if (searchEmail)
                users = Membership.FindUsersByEmail(searchText);
            else
                users = Membership.FindUsersByName(searchText);
        }
        else
        {
            users = allUsers;
        }

        gvwUsers.DataSource = users;
        gvwUsers.DataBind();

    }
    protected void rptAlphabet_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        gvwUsers.Attributes.Add("SearchByEmail", false.ToString());
        if (e.CommandArgument.ToString().Length == 1)
        {
            gvwUsers.Attributes.Add("SearchText", e.CommandArgument.ToString() + "%");
            BindUser(false);
        }
        else
        {
            gvwUsers.Attributes.Add("SearchText", "");
            BindUser(false);
        }
    }
    protected void gvwUsers_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = e.Row.Cells[6].Controls[0] as ImageButton;
            btn.OnClientClick = "if (confirm('Are you sure you want to delete this user account?') == false) return false;";
        }
    }


    protected void gvwUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string userName = gvwUsers.DataKeys[e.RowIndex].Value.ToString();
        ProfileManager.DeleteProfile(userName);
        Membership.DeleteUser(userName);
        BindUser(true);
        lblTotUsers.Text = allUsers.Count.ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool searchByEmail = (ddlSearchTypes.SelectedValue == "E-mail");
        gvwUsers.Attributes.Add("SearchText", "%" + txtSearchText.Text + "%");
        gvwUsers.Attributes.Add("SearchByEmail", searchByEmail.ToString());
        BindUser(false);
    }
}
